package com.grantheath.erebus.view.preloaders
{
	import com.grantheath.erebus.view.interfaces.IErebusTransitionHandeler;
	import mx.containers.Canvas;
	
	import com.grantheath.erebus.view.interfaces.IErebusPagePreloader;
	import com.grantheath.erebus.view.helpers.ErebusPageEvent;
	import com.grantheath.erebus.model.interfaces.IErebusAssetNode;
	import com.grantheath.erebus.model.interfaces.IErebusPageNode;

	import flash.display.DisplayObjectContainer;

	/**
	 * @author grantheath
	 */
	public class ErebusFlexPagePreloader extends Canvas implements IErebusPagePreloader
	{
		private var _pageNode : IErebusPageNode;
		private var _preloaderLayer : DisplayObjectContainer;
		private var _isTransitioning : Boolean;
		private var _transitionInterupted : Boolean;
		private var _transitionHandeler : IErebusTransitionHandeler;

		public function initializePreloader(node : IErebusPageNode) : void
		{
			var transitionHandelerClass : Class = node.transitionHandelerClass;
			
			_pageNode = node;
			_preloaderLayer = node.preloaderLayer;
			_transitionHandeler = new transitionHandelerClass(this, _preloaderLayer);
			isTransitioning = false;
			transitionInterupted = false;
		}

		public function unitializePreloader() : void
		{
		}

		public function onPageOpen(pageNode : IErebusPageNode = null) : void
		{
		}

		public function onPageComplete(pageNode : IErebusPageNode = null) : void
		{
			if(pageNode) {
				if(pageNode.pageAssets.length == 0) {
					dispatchEvent(new ErebusPageEvent(ErebusPageEvent.PRELOADER_LOAD_COMPLETE));
				}
			}
		}

		public function onPageProgress(pageNode : IErebusPageNode = null) : void
		{
		}

		public function onPageError(pageNode : IErebusPageNode = null) : void
		{
		}

		public function onAssetOpen(assetNode : IErebusAssetNode = null) : void
		{
		}

		public function onAssetComplete(assetNode : IErebusAssetNode = null) : void
		{
			if(assetNode) {
				var pageNode : IErebusPageNode = assetNode.pageNode;
				var isLoaded : Boolean = true;
				
				for each(var asset : IErebusAssetNode in pageNode.pageAssets) {
					if(!asset.asset) {
						isLoaded = false;
						break;
					}
				}
				if(isLoaded) {
					dispatchEvent(new ErebusPageEvent(ErebusPageEvent.PRELOADER_LOAD_COMPLETE));
				}
			}
		}

		public function onAssetProgress(assetNode : IErebusAssetNode = null) : void
		{
		}

		public function onAssetError(assetNode : IErebusAssetNode = null) : void
		{
		}

		public function get pageNode() : IErebusPageNode
		{
			return _pageNode;
		}

		public function get preloaderLayer() : DisplayObjectContainer
		{
			return _preloaderLayer;
		}
		
		public function get isTransitioning() : Boolean
		{
			return _isTransitioning;
		}
		
		public function set isTransitioning(isTransitioning : Boolean) : void
		{
			_isTransitioning = isTransitioning;
		}
		
		public function get transitionInterupted() : Boolean
		{
			return _transitionInterupted;
		}
		
		public function set transitionInterupted(transitionInterupted : Boolean) : void
		{
			_transitionInterupted = transitionInterupted;
		}
		
		public function get transitionHandeler() : IErebusTransitionHandeler
		{
			return _transitionHandeler;
		}
	}
}
